import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, OnInit, ViewChild, Input } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { ActivatedRoute } from '@angular/router';
import { AppComponentBase } from '@shared/common/app-component-base';
import { TaskServiceProxy, WorkflowServiceProxy, OutsourcingServiceProxy, OutsourcingWorkflowServiceProxy, UserServiceProxy, ProcessingOrderDto, ProcessingViewDto } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper';

@Component({
    selector: 'ProcessingOrderDetail',
    templateUrl: './ProcessingOrderDetail.component.html',
    styleUrls: ['./ProcessingOrderDetail.component.css']
})
export class ProcessingOrderDetailComponent extends AppComponentBase implements OnInit {
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('taskdataTable') taskdataTable: Table;
    @ViewChild('dataTable') dataTable: Table;

    @Input() workflowid: any;
    @Output() out = new EventEmitter();

    active = false;
    saving = false;
    _processingOrderDto: ProcessingOrderDto = new ProcessingOrderDto();
    _processingViewDto: ProcessingViewDto[];
    detailsprimengTableHelper: PrimengTableHelper;
    ngOnInit(): void {

        console.log(this.workflowid);
        this.initForm();
        //this.GetApprovalProcessRecord(this.workflowid);
    }
    constructor(injector: Injector,
        private route: ActivatedRoute,
        private _OutsourcingWorkFlowServiceProxy: OutsourcingWorkflowServiceProxy,
        private _OutsouringServiceProxy: OutsourcingServiceProxy,
        private _taskServiceProxy: TaskServiceProxy,
        private _userServiceProxy: UserServiceProxy,
        private _fileDownloadService: FileDownloadService,
        private _workflowService: WorkflowServiceProxy) {
        super(injector);
        this.detailsprimengTableHelper = new PrimengTableHelper();

    }

    initForm(): void {
        this._OutsourcingWorkFlowServiceProxy.getProcessingByWorkFlowId(this.workflowid).subscribe(dto => {
            this._processingOrderDto = dto.processingOrderDto;
            this._processingViewDto = dto.processingViewDtos;
            console.log(dto);
            this.primengTableHelper.totalRecordsCount = this._processingViewDto.length;
            this.primengTableHelper.records = this._processingViewDto;
            this.primengTableHelper.hideLoadingIndicator();

            _.forEach(this._processingViewDto, viewItem => {
                console.log(viewItem.taskForModelId);
                this._taskServiceProxy.getTaskForEdit(viewItem.taskForModelId)
                    .subscribe(userResult => {
                        console.log(userResult);
                        viewItem.taskCode = userResult.taskCode;
                        viewItem.taskName = userResult.drawingName;
                        viewItem.picNo = userResult.drawingCode;
                    });
                this._OutsouringServiceProxy.getOutsourcingApplyAmountForTaskId(viewItem.taskForModelId).subscribe(result => {
                    viewItem.outsourcingAmount = result;
                });
                this.active = true;
            });
        });
    }

    GetApprovalProcessRecord(workflowId: string) {
        this._workflowService.getExecutionPointer(workflowId).subscribe(dto => {
            this.detailsprimengTableHelper.totalRecordsCount = dto.length;
            this.detailsprimengTableHelper.records = dto;
            this.detailsprimengTableHelper.hideLoadingIndicator();
            _.forEach(dto, viewItem => {
                this._userServiceProxy.getUserInfo(viewItem.extensionAttributes.AssignedPrincipal)
                    .subscribe(userResult => {
                        viewItem.auditUserName = userResult.name;
                    });
            });
        });
    }

    exportToexcel(): void {
        this._OutsourcingWorkFlowServiceProxy.prossingOrderForExcel(this.workflowid).subscribe(dto => {
            this._fileDownloadService.downloadTempFile(dto);
        });
    }
}

